package com.biscuit.page.basicservice.persistence.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biscuit.page.basicservice.model.entities.mongo.DefaultCollection;
import com.biscuit.page.basicservice.model.entities.mysql.SysCollection;
import com.biscuit.page.basicservice.model.enums.BaseEntityEnum;
import com.biscuit.page.basicservice.persistence.mapper.CollectionMapper;
import com.biscuit.page.basicservice.service.api.MongoService;
import com.biscuit.page.basicservice.utils.NumberUtil;
import com.biscuit.page.basicservice.utils.WrapperUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author mazihao
 * @createTime 2024/3/26 9:27
 */
@Component
public class CollectionDao extends ServiceImpl<CollectionMapper, SysCollection> {

    @Resource
    private CollectionMapper collectionMapper;

    @Resource
    private MongoService mongoService;

    public List<SysCollection> getListByUserId(Long userId) {
        LambdaQueryWrapper<SysCollection> wrapper = WrapperUtil.instanceQuery(SysCollection.class)
                .eq(SysCollection::getUserId, userId)
                .orderByAsc(SysCollection::getOrdered)
                .orderByDesc(SysCollection::getCreateTime);
        return super.list(wrapper);
    }

    public void updateDeleteById(Long id) {
        LambdaUpdateWrapper<SysCollection> updateWrapper = Wrappers.lambdaUpdate(SysCollection.class)
                .set(SysCollection::getIsDeleted, 1)
                .set(SysCollection::getDeleteTime, LocalDateTime.now())
                .eq(SysCollection::getId, id);
        super.update(updateWrapper);
    }

    public SysCollection saveAccountCollection(SysCollection collection) {
        // 判断新增和修改
        if (NumberUtil.numberCheck(collection.getId())) {
            super.updateById(collection);
        } else {
            this.collectionMapper.insertAndReturn(collection);
        }
        return collection;
    }

    public void saveDefaultCollection(DefaultCollection collection) {
        // 判断新增和修改
        if (StringUtils.isNotBlank(collection.getId())) {
            this.mongoService.updateById(collection);
        } else {
            this.mongoService.insert(collection);
        }
    }

    public int getCountByUserId(Long userId) {
        LambdaQueryWrapper<SysCollection> queryWrapper = Wrappers.lambdaQuery(SysCollection.class)
                .eq(SysCollection::getIsDeleted, BaseEntityEnum.NOT_DELETED.getValue())
                .eq(SysCollection::getUserId, userId);
        long count = super.count(queryWrapper);
        return NumberUtil.numberCheck(count) ? (int) count : 0;
    }
}
